<template>
	<el-container>
		<el-header style="height: auto;">
			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-main class="nopadding">
<!--			<el-input v-model="parentMsg"></el-input>-->
			<!--			<scTable ref="table" :apiObj="apiObj" row-key="runId" stripe remoteSort remoteFilter>-->
			<!--				<el-table-column label="#" type="index" width="50"></el-table-column>-->
			<!--				<el-table-column label="流水号" prop="id" width="150"></el-table-column>-->
			<!--				<el-table-column label="流程标题" prop="flowTitle" width="150"></el-table-column>-->
			<!--				<el-table-column label="申请人" prop="userName" width="150"></el-table-column>-->
			<!--				&lt;!&ndash;				<el-table-column label="部门" prop="deptName" width="150"></el-table-column>&ndash;&gt;-->
			<!--				<el-table-column label="申请人部门" prop="userDeptName" width="150"></el-table-column>-->
			<!--				<el-table-column label="审批人" prop="approverUserName" width="150"></el-table-column>-->
			<!--				<el-table-column label="审批人部门" prop="approverDeptName" width="150"></el-table-column>-->
			<!--				<el-table-column label="外出开始时间" prop="outTime" width="150" :show-overflow-tooltip="true"></el-table-column>-->
			<!--				<el-table-column label="外出结束时间" prop="endTime" width="150" :show-overflow-tooltip="true"></el-table-column>-->
			<!--				<el-table-column label="外出地点" prop="outAddress" width="200" :show-overflow-tooltip="true"></el-table-column>-->
			<!--				<el-table-column label="外出事由" prop="reason" width="300" :show-overflow-tooltip="true"></el-table-column>-->
			<!--				<el-table-column label="操作" fixed="right" align="right" width="100">-->
			<!--					<template #default="scope">-->
			<!--						<el-button-group>-->
			<!--							<el-button text type="primary" size="small" @click="readBpmDetails(scope.row,scope.$index)">审批</el-button>-->
			<!--						</el-button-group>-->
			<!--					</template>-->
			<!--				</el-table-column>-->
			<!--			</scTable>-->
			<el-table ref="table" :data="apiObj">
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<!-- 电子签名列 -->
				<el-table-column label="电子签名" align="center">
					<template v-slot="scope">
						<div v-if="scope.row.signatureUrl">
							<el-image
								style="width: 80px; height: 40px; cursor: pointer"
								:src="scope.row.signatureUrl"
								:preview-src-list="[scope.row.signatureUrl]"
								fit="contain"
								close-on-click-modal
							preview-teleported
							:z-index="9999"
							>
							</el-image>
						</div>
						<span v-else class="unsigned-text">未签署</span>
					</template>
				</el-table-column>
				<!--				<el-table-column label="流水号" prop="id" width="150"></el-table-column>-->
				<!--				<el-table-column label="流程标题" prop="flowTitle" width="150"></el-table-column>-->
				<el-table-column label="申请人" prop="applicantName" width="150"></el-table-column>
				<!--				<el-table-column label="部门" prop="deptName" width="150"></el-table-column>-->
				<el-table-column label="申请人部门" prop="applicantDeptName" width="150"></el-table-column>
				<el-table-column label="审批人" prop="approverName" width="150"></el-table-column>
				<el-table-column label="审批人部门" prop="approverDeptName" width="150"></el-table-column>
				<el-table-column label="外出开始时间" prop="boutattend.d83800" width="150"
								 :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="外出结束时间" prop="boutattend.d83801" width="150"
								 :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="外出地点" prop="boutattend.d66126" width="200"
								 :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="外出事由" prop="boutattend.d89268" width="300"
								 :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="状态" prop="stats" width="150">
					<template #default="scope">
						{{
							scope.row.stats === undefined ? '--' :
								scope.row.stats == '0' ? '审核中' :
									scope.row.stats == '1' ? '通过' :
										scope.row.stats == '2' ? '不通过' : '--'
						}}
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="100">
					<template #default="scope">
						<el-button-group v-if="scope.row.stats === '0'">
							<el-button text type="primary" size="small" @click="readBpmDetails(scope.row,scope.$index)">
								审批
							</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</el-table>

<!--			<electronic-signatures :msg="parentMsg" @signature-uploaded="handleSignatureUploaded"-->
<!--								   @editParentMsg="handleEditParentMsg"></electronic-signatures>-->
		</el-main>

		<el-dialog title="审批" v-model="dialogVisible" width="30%">
			<el-form :model="form" label-width="80px">
				<el-form-item label="审批结果">
					<el-radio-group v-model="form.stats">
						<el-radio label="1">同意</el-radio>
						<el-radio label="2">拒绝</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="备注">
					<el-input v-model="form.remark" type="textarea"></el-input>
				</el-form-item>
				<div>
					<spersonalFinance
						ref="signaturePad"
						:line-width="4"
						:line-color="'#409EFF'"
						@save="handleSave"
						@reset="reset"
						@error="handleError"
					></spersonalFinance>
				</div>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="dialogVisible = false">取消</el-button>
					<el-button type="primary" @click="submitForm">确定</el-button>
				</span>
			</template>
		</el-dialog>
	</el-container>
</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import electronicSignatures from '@/views/electronicSignatures/electronicSignatures.vue' // 确保路径正确
import spersonalFinance from '@/components/scpersonalFinance/index.vue'
import {FINANCE_CODES} from '@/config/statusCodes'

export default {
	name: 'My Attend Record',
	components: {
		scSelectFilter,
		electronicSignatures,
		spersonalFinance
	},
	data() {
		return {
			parentMsg: '你好',
			aaaaa: 'http://oa-oss-attachment.oss-cn-beijing.aliyuncs.com/purchase/dianziqianming/5903485542227.png',
			resultImg:'',
			dialog: {
				save: false,
				detailsSave: false
			},
			dialogVisible: false,
			form: {
				result: '',
				remark: ''
			},
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				}
			],
			apiObj: [],
			search: {
				date: ''
			}
		}
	},
	created() {
		// let userInfo = this.$TOOL.data.get("USER_INFO");
		// console.log("111111111111111111111111", userInfo)
		// this.$API.office.attend.getMyOutAttendList.get({userId: userInfo.id}).then(res => {
		// 	this.apiObj = res.data;
		// })
	},
	mounted() {
		this.getDataList();
	},
	methods: {
		reset(){
			this.resultImg = ''
		},
		// 处理签名保存
		handleSave({ base64, file }) {
			// 自定义上传逻辑
			const formData = new FormData()
			formData.append('file', file)
			formData.append('code', FINANCE_CODES.EGRESS)

			this.$API.office.personalFinance.uploadFileByCode.post(formData)
				.then(res => {
					this.resultImg  = res.redirect
				})
				.catch(err => {
					this.$message.error('文件上传失败')
				})
		},
// 处理错误
		handleError(message) {
			this.$message.error(message)
		},
		handleSignatureUploaded(formData) {
			// localStorage.setItem('signatureFormData',formData);
			console.log("formData", formData)
			this.form.signatureFormData = formData; // 接收签名 formData 并存储在表单中
		},
		handleEditParentMsg(parentMsg) {
			this.parentMsg = parentMsg;
		},
		getDataList() {
			let userInfo = this.$TOOL.data.get("USER_INFO");
			this.$API.office.attend.approversQueryAttendanceData.get({
				option2: userInfo.id,
				status: "203",//外出
				page: this.search.page,
				pageSize: this.search.pageSize
			}).then(res => {
				if (res.code === 200) {
					this.apiObj = res.data.list;
				}
			})
		},
		filterChange(data) {
			this.search.date = data.date;
			this.$refs.table.upData(this.search)
		},
		readBpmDetails(row, index) {
			this.row = row;
			this.dialogVisible = true;
			// 清空表单数据
			this.form.result = '';
			this.form.remark = '';
		},
		submitForm() {
			if (
				typeof this.resultImg === 'undefined' ||
				this.resultImg === null ||
				(typeof this.resultImg === 'string' && this.resultImg.trim() === '')
			) {
				this.$message.error('请生成签名后或字迹规范后审批');
				return;
			}
			let params = {
				stats: this.form.stats,
				correlationId: this.row.correlationId,
				remark: this.form.remark,
				signatureUrl:this.resultImg
			}
			this.$API.system.attendConfig.approveOut.get(params).then(res => {
				this.getDataList();
				// 处理表单提交逻辑
				console.log('审批结果:', this.form.result);
				console.log('备注:', this.form.remark);
				this.dialogVisible = false;
			})

		}
	}
}
</script>

<style>
</style>
